<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class Me extends Controller
{

    /**
     * 获取我的个人信息
     */
    public function info (Request $req)
    {
        \Log::info(config('app.url'));
        return $this->resSuccess('我的信息获取成功。', $req->user());
    }



    /**
     * 保存个人信息
     * @param  name  名字
     * @param password
     * */
    public function saveInfo (Request $req)
    {
        $user = $req->user();
        $user->name = $req->name;
        $user->img = $req->img;
        if ($pwd  = $req->post('password')) {
            $user->password = Hash::make(trim($pwd));
        }

        $user->save();

        return $this->resSuccess('用户资料保存成功。');
    }

    /**
     * 上传用户头像
     */
    public function uploadImg(Request $req) {

        if (! $file = $req->file('img')) {
            return $this->resFail('请上传图片');
        }

        //上传文件到 /storage/app/public
        //php artisan storage:link
        $res =  $file->store('user','public'); //public： filesystem 配置项

        return $this->resSuccess('上传成功',
            config('app.url').'storage/'.$res
        );
    }

}